package it.acweb.pauseless.pauselessapp;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class SongsAdapter extends ArrayAdapter<Song> {
	
	private SongsAdapter songsAdapter = null;
	private Context context;
	private Song[] items;
	int layoutResourceId; 
	
	public SongsAdapter(Context context, int layoutResourceId, Song[] values) {
	    super(context, layoutResourceId, values);
	    this.songsAdapter = this;
	    this.context = context;
	    this.layoutResourceId = layoutResourceId;
	    this.items = values;
	}
	
    public View getView(int position, View convertView, ViewGroup parent) {
		View row = convertView;
		ViewHolder holder = null;
		
		if (row == null) {
		    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		    row = inflater.inflate(layoutResourceId, parent, false);
		    holder = new ViewHolder();
		    holder.firstLine = (TextView) row.findViewById(R.id.firstLine);
		    holder.secondLine = (TextView) row.findViewById(R.id.secondLine);
		    holder.pauses = (TextView) row.findViewById(R.id.pauses);
		    holder.imgIcon = (ImageView) row.findViewById(R.id.imgIcon);
		    
		    row.setTag(holder);
		} else {
			holder = (ViewHolder)convertView.getTag();
		}
		    

	    final Song song = items[position];
	    holder.firstLine.setText(song.get_path() + "/" + song.get_track_no() + " " + song.get_artist());
	    holder.secondLine.setText(song.get_title() + " (" + song.get_bpm() + ")");
	    holder.pauses.setText(song.get_pauses_string());
	    holder.imgIcon.setImageResource(R.drawable.add);

	    holder.imgIcon.setOnClickListener(new OnClickListener() {
	        @Override
	        public void onClick(View v) {
	            xml_utils.alert("qui");
	        }
	    });

	    return row;

	}
	
	public static class ViewHolder {
		public TextView firstLine;// = null;
		public TextView secondLine;// = null;
		public TextView pauses;// = null;
		public ImageView imgIcon;
	}

}
